package com.heer.gymnasium.module.order.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heer.amap.utils.AMapUtil;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.card.beans.CardListResponseBean;
import com.heer.gymnasium.module.coach.beans.CoachOrderBean;
import com.heer.gymnasium.module.order.beans.AddOrderBean;
import com.heer.gymnasium.module.order.beans.CardDetialResponseBean;
import com.heer.gymnasium.module.order.beans.MyOrderBean;
import com.heer.gymnasium.module.order.beans.OrderBean;
import com.heer.gymnasium.module.order.beans.OrderInfoResponseBean;
import com.heer.gymnasium.module.order.beans.OrderResponseBean;
import com.heer.gymnasium.module.team.beans.ActivityHistoryResponseBean;
import com.heer.gymnasium.util.PayUtils;
import com.heer.gymnasium.util.StringUtils;
import com.heer.gymnasium.wxapi.beans.WxPayBean;
import com.heer.imagepick.DensityUtils;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.view.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

import static com.heer.gymnasium.constant.Type.ORDER_CARD;

/**
 * Created by yan
 * on 2018/1/26
 * 我的订单支付界面
 */
public class OrderActivity extends BaseActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_order;
    private static final int RES_ID_TYPENAME_TV = R.id.layout_order_typename_tv;
    // 订单信息区域
    private static final int RES_ID_INFO_RL = R.id.layout_order_info_rl;
    private static final int RES_ID_CONTENT_LL = R.id.layout_order_content_ll;
    private static final int RES_ID_CARDNAME_TV = R.id.layout_order_cardname_tv;
    private static final int RES_ID_STORENAME_TV = R.id.layout_order_storename_tv;
    private static final int RES_ID_LIMIT_TV = R.id.layout_order_limit_tv;
    private static final int RES_ID_COUNT_TV = R.id.layout_order_count_tv;
    private static final int ORDER_MONEY_ID = R.id.layout_order_money_tv;
    //   网格区域
    private static final int RES_ID_RECHARGE_GV = R.id.layout_order_gv;
    // 购买数量区域
    private static final int RES_ID_PURCHASE_ll = R.id.layout_order_purchase_num_ll;
    private static final int RES_ID_DESCREASE_IV = R.id.layout_order_decrease_iv;
    private static final int RES_ID_NUMBER_TV = R.id.layout_order_purchase_num_tv;
    private static final int RES_ID_ADD_IV = R.id.layout_order_add_iv;
    //    须知区域
    private static final int RES_ID_INSTRUCTION_LL = R.id.layout_order_instruction_ll;
    private static final int RES_ID_TYPE_TV = R.id.layout_order_instruction_typename_tv;
    private static final int RES_ID_CONTENT_TV = R.id.layout_order_instruction_content_tv;
    private static final int RES_ID_CONFIRM_CB = R.id.layout_order_confirm_cb;
    //    支付区域
    private static final int RES_ID_PAY_INFO_TV = R.id.layout_order_payinfo_tv;
    private static final int RES_ID_PAY_NUM_TV = R.id.layout_order_total_number_tv;
    private static final int RES_ID_PAY_TOTAL_MONEY_TV = R.id.layout_order_total_money_tv;
    private static final int RES_ID_PAY_WACHAT_RB = R.id.layout_order_wachat_rb;
    private static final int RES_ID_PAY_ALIPAY_RB = R.id.layout_order_alipay_rb;
    private static final int RES_ID_PAY_BALANCE_RB = R.id.layout_order_balance_rb;
    private static final int RES_ID_PAY_BALANCE_TV = R.id.layout_order_balance_tv;
    private static final int RES_ID_PAY_TV = R.id.layout_order_pay_tv;
    //    留言备注
    private static final int RES_ID_REMARK = R.id.layout_order_remark_et;
    private static final int RES_ID_REMARK_LL = R.id.layout_order_remark_ll;

    //    充值部分网格Item
    private static final int RES_ID_ITEM = R.layout.item_recharge_money;
    private static final int RES_ID_ITEM_LL = R.id.item_recharge_money_ll;
    private static final int RES_ID_ITEM_LABEL_TV = R.id.item_recharge_money_label_tv;
    private static final int RES_ID_ITEM_MONEY_TV = R.id.item_recharge_money_detial_tv;

    //    所选订场列表Item
    private static final int RES_ID_ITEM_STADIUM = R.layout.item_order_detail_list;
    private static final int RES_ID_ITEM_STADIUM_NAME = R.id.item_order_detail_list_storename_tv;
    private static final int RES_ID_ITEM_STADIUM_PLACE = R.id.item_order_detail_list_placename_tv;
    private static final int RES_ID_ITEM_STADIUM_TIME = R.id.item_order_detail_list_time_tv;
    private static final int RES_ID_ITEM_STADIUM_MONEY = R.id.item_order_detail_list_money_tv;
    private static final int RES_ID_ITEM_STADIUM_GREY_LINE = R.id.item_order_detail_list_gray_v;
    private Activity $this;

    public interface OrderType {
        String from_card = "card";
        String from_coupon = "coupon";
        String from_recharge = "recharge";
        String from_coach = "coach";
        String from_order = "order";
    }

    private String from, storeId;

    @BindView(RES_ID_INFO_RL)
    RelativeLayout infoView;
    @BindView(RES_ID_TYPENAME_TV)
    TextView typeNameTextView;

    @BindView(RES_ID_RECHARGE_GV)
    MyGridView gridView;

    //    须知区域
    @BindView(RES_ID_INSTRUCTION_LL)
    LinearLayout instructionView;
    @BindView(RES_ID_TYPE_TV)
    TextView instructionNameTextView;
    @BindView(RES_ID_CONTENT_TV)
    TextView contentTextView;
    @BindView(RES_ID_CONFIRM_CB)
    CheckBox confirmCheckBox;

    // 购买区域
    @BindView(RES_ID_PURCHASE_ll)
    LinearLayout purchaseView;
    @BindView(RES_ID_NUMBER_TV)
    TextView purchaseNumTextView;

    //支付区域
    @BindView(RES_ID_PAY_INFO_TV)
    TextView payInfoTextView;
    @BindView(RES_ID_PAY_NUM_TV)
    TextView totalNumTextView;
    @BindView(RES_ID_PAY_TOTAL_MONEY_TV)
    TextView totalMoneyTextView;
    @BindView(RES_ID_PAY_WACHAT_RB)
    RadioButton wachatRadioButton;
    @BindView(RES_ID_PAY_ALIPAY_RB)
    RadioButton alipayRadioButton;
    @BindView(RES_ID_PAY_BALANCE_RB)
    RadioButton balanceRadioButton;
    @BindView(RES_ID_PAY_BALANCE_TV)
    TextView balanceTextView;

    //    顶部信息部分
    @BindView(RES_ID_CARDNAME_TV)
    TextView cardNameTextView;
    @BindView(RES_ID_STORENAME_TV)
    TextView storeNameTextView;
    @BindView(RES_ID_LIMIT_TV)
    TextView limitTextView;
    @BindView(RES_ID_COUNT_TV)
    TextView countTextView;
    @BindView(ORDER_MONEY_ID)
    TextView moneyTextView;

    @BindView(RES_ID_REMARK)
    EditText remarkEditText;
    @BindView(RES_ID_REMARK_LL)
    LinearLayout remarkLinearLayout;

    private List<CardDetialResponseBean> data;
    private List<MyOrderBean.DetialStadium> stadiumDetialData;
    private Map<Integer, Boolean> selectStatus;
    QuickTypeAdapter adapter;
    private AddOrderBean addOrderBean = new AddOrderBean();
    //  orderType  1:订一个场地,2:定多个场地,3:充值,4:购卡,5:私教，6:约战  7:拼单,8自动续订   unitPrice单价，私教部分用到
    private String orderType = "1";

    //剩余余额 支付金额
    private double account = 0.0, paymentAmount = 0.0, unitPrice = 0.0;

    private String cardId = "";

    //    页面跳转
    public static void startActivity(Context context, String from, String id, String orderType) {
        Intent intent = new Intent(context, OrderActivity.class);
        intent.putExtra(EXTRA_FROM, from);
        intent.putExtra(EXTRA_ID, id);
        intent.putExtra(EXTRA_TYPE, orderType);
        context.startActivity(intent);
    }

    //    页面跳转 普通订场
    public static void startActivity(Context context, String from, String id, String orderType, boolean isLeave) {
        Intent intent = new Intent(context, OrderActivity.class);
        intent.putExtra(EXTRA_FROM, from);
        intent.putExtra(EXTRA_ID, id);
        intent.putExtra(EXTRA_TYPE, orderType);
        intent.putExtra(EXTRA_DATA, isLeave);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
        data = new ArrayList<>();
        selectStatus = new HashMap<>();
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.startMainActivity(this);
        from = getIntent().getStringExtra(EXTRA_FROM);
        storeId = getIntent().getStringExtra(EXTRA_ID);
        if (!TextUtils.isEmpty(from)) {
            switch (from) {
                case OrderType.from_card:
                    orderType = ORDER_CARD;
                    fromCardInit();
                    getCardInfo();
                    break;
                case OrderType.from_recharge:
                    orderType = Type.ORDER_RECHARGE;
                    fromRechargeInit();
                    getRechargeCardInfo();
                    break;

                case OrderType.from_coach:
                    orderType = Type.ORDER_COACH;
                    fromCoachInit();
                    getCoachOrderInfo();
                    break;
                case OrderType.from_coupon:
                    fromCouponInit();
                    getOrderInfo(storeId);
                    break;
                case OrderType.from_order:
                    fromOrderInit();
                    break;
                default:
                    fromCouponInit();
                    getOrderInfo(storeId);
                    break;
            }
        }

    }

    @OnClick({RES_ID_ADD_IV, RES_ID_DESCREASE_IV})
    public void onClickForAddOrDecrease(View view) {
        int num = StringUtils.toInt(purchaseNumTextView.getText().toString(), 1);
        switch (view.getId()) {
            case RES_ID_ADD_IV:
                num++;
                purchaseNumTextView.setText(num + "");
                totalNumTextView.setText("共" + num + "件");
                totalMoneyTextView.setText(unitPrice * num + "");
                break;
            case RES_ID_DESCREASE_IV:
                if (num > 1) {
                    num--;
                    purchaseNumTextView.setText(num + "");
                    totalNumTextView.setText("共" + num + "件");
                    totalMoneyTextView.setText(unitPrice * num + "");
                }
                break;
        }
        paymentAmount = num * unitPrice;
        addOrderBean.setNum(purchaseNumTextView.getText().toString());
    }

    @OnClick(RES_ID_PAY_TV)
    public void onClickForPay() {//订场和待付款进入的直接支付，其余下单后支付
        if (from.equals(OrderType.from_coupon)) {
            leaveMesssage(storeId);
        }
        if (from.equals(OrderType.from_order) || from.equals(OrderType.from_coupon)) {
            if (confirmCheckBox.isChecked()) {
                if (balanceRadioButton.isChecked()) {
                    payOrder(storeId);
                } else if (alipayRadioButton.isChecked()) {
                    getAlipayOrderInfo(storeId);
                } else {
                    wachatPayOrder(storeId);
                }
            } else {
                ToastUtil.show(this, "请勾选充值须知");
            }
            return;
        }
        if (orderType.equals(Type.ORDER_COACH)) {
            if (purchaseNumTextView.getText().equals("0")) {
                ToastUtil.show(this, "数量不能为0");
                return;
            }
            order();
        } else if (orderType.equals(ORDER_CARD) || orderType.equals(Type.ORDER_RECHARGE)) {
            if (confirmCheckBox.isChecked()) {
                order();
            } else {
                ToastUtil.show(this, "请勾选充值须知");
            }
        }
    }

    //购卡充值初始信息
    private void fromRechargeInit() {
        balanceRadioButton.setVisibility(View.GONE);
        balanceTextView.setVisibility(View.GONE);
        infoView.setVisibility(View.GONE);
        gridView.setVisibility(View.VISIBLE);
        typeNameTextView.setText("充值内容");
        instructionNameTextView.setText("充值须知");
        payInfoTextView.setText("总金额");
        totalNumTextView.setText("共1件");
        adapter = new QuickTypeAdapter<CardDetialResponseBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, CardDetialResponseBean data, int type, int pos) {
                fillRechargeData(holder, data);
            }

            @Override
            public void bindListener4View(ViewHolder holder, final CardDetialResponseBean item, int type, final int pos) {
                super.bindListener4View(holder, item, type, pos);
                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        for (int i = 0; i < data.size(); i++) {
                            if (i == pos) {
                                data.get(i).setSelect(true);
                            } else {
                                data.get(i).setSelect(false);
                            }
                        }
                        setAddOrderBean(item);
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        };
        gridView.setAdapter(adapter);
    }

    //购卡初始信息
    private void fromCardInit() {
        titleUtils.setTitleText("我的订单");
        infoView.setVisibility(View.VISIBLE);
        gridView.setVisibility(View.GONE);
        typeNameTextView.setText("订单内容");
        instructionNameTextView.setText("购买须知");
        confirmCheckBox.setText("购买须知已确认");
        payInfoTextView.setText("支付信息");
        totalNumTextView.setText("共1件");
    }

    //私教卡初始信息
    private void fromCoachInit() {
        titleUtils.setTitleText("我的订单");
        infoView.setVisibility(View.VISIBLE);
        gridView.setVisibility(View.GONE);
        purchaseView.setVisibility(View.VISIBLE);
        instructionView.setVisibility(View.GONE);
        cardNameTextView.setVisibility(View.GONE);
        typeNameTextView.setText("订单内容");
        payInfoTextView.setText("支付信息");
    }

    //私教卡初始信息
    private void fromCoachInitForOrder() {
        titleUtils.setTitleText("我的订单");
        infoView.setVisibility(View.VISIBLE);
        gridView.setVisibility(View.GONE);
        purchaseView.setVisibility(View.GONE);
        instructionView.setVisibility(View.VISIBLE);
        countTextView.setVisibility(View.GONE);
        typeNameTextView.setText("订单内容");
        payInfoTextView.setText("支付信息");
        storeNameTextView.setText("私教卡");
    }

    //充值卡来自订单列表
    private void fromRechargeInitForOrder() {
        titleUtils.setTitleText("我的订单");
        infoView.setVisibility(View.VISIBLE);
        gridView.setVisibility(View.GONE);
        purchaseView.setVisibility(View.GONE);
        instructionView.setVisibility(View.VISIBLE);
        cardNameTextView.setVisibility(View.VISIBLE);
        limitTextView.setVisibility(View.GONE);
        countTextView.setVisibility(View.GONE);
        balanceRadioButton.setVisibility(View.GONE);
        balanceTextView.setVisibility(View.GONE);
        instructionNameTextView.setText("购买须知");
        typeNameTextView.setText("订单内容");
        payInfoTextView.setText("支付信息");
        cardNameTextView.setText("充值卡");
        totalNumTextView.setText("共1件");
    }

    //订场进入订单详情初始信息
    private void fromCouponInit() {
        titleUtils.setTitleText("我的订单");
        infoView.setVisibility(View.GONE);
        gridView.setVisibility(View.VISIBLE);
        if (getIntent().getBooleanExtra(EXTRA_DATA, false)) {
            remarkLinearLayout.setVisibility(View.VISIBLE);
        }
        typeNameTextView.setText("订单内容");
        instructionNameTextView.setText("购买须知");
        confirmCheckBox.setText("购买须知已确认");
        payInfoTextView.setText("支付信息");
//        bean = new OrderBean();
        gridView.setNumColumns(1);
        gridView.setVerticalSpacing(DensityUtils.dip2px(this, 10));
        gridView.setPadding(DensityUtils.dip2px(this, 15), DensityUtils.dip2px(this, 10),
                DensityUtils.dip2px(this, 15), DensityUtils.dip2px(this, 10));
    }

    //订单页面进入的初始化信息
    private void fromOrderInit() {
        if (TextUtils.isEmpty(storeId)) {
            ToastUtil.show($this, "订单不存在");
            finish();
            return;
        }
        getOrderInfo(storeId);
    }

    //填充充值卡数据
    private void fillRechargeData(ViewHolder holder, CardDetialResponseBean data) {
        if (data.isSelect()) {
            holder.setBg(RES_ID_ITEM_LL, R.drawable.yellow_corners_radius_5);
            holder.setTextColor(RES_ID_ITEM_LABEL_TV, getResources().getColor(R.color.font_yellow));
            holder.setTextColor(RES_ID_ITEM_MONEY_TV, getResources().getColor(R.color.font_yellow));
            contentTextView.setText(StringUtils.fromHtml(data.getNotice()));
            totalMoneyTextView.setText(data.getMoney());
        } else {
            holder.setBg(RES_ID_ITEM_LL, R.drawable.gray_corners_radius_5);
            holder.setTextColor(RES_ID_ITEM_LABEL_TV, getResources().getColor(R.color.font_gray));
            holder.setTextColor(RES_ID_ITEM_MONEY_TV, getResources().getColor(R.color.font_gray));
        }
        holder.setText(RES_ID_ITEM_MONEY_TV, "¥" + data.getMoney() + "赠¥" + data.getGive_money());

    }

    //获得充值卡列表信息
    private void getRechargeCardInfo() {
        if (TextUtils.isEmpty(storeId)) {
            finish();
        }
        mAppHelper.apiService().getRechargeCardInfo(storeId).enqueue(new BaseCallback<ApiResponse<List<CardDetialResponseBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<CardDetialResponseBean>>> call, Response<ApiResponse<List<CardDetialResponseBean>>> response) {
                List<CardDetialResponseBean> itemList = response.body().getData();
                if (itemList == null || itemList.size() == 0) {
                    finish();
                    ToastUtil.show($this, "暂无充值卡");
                } else {
                    data.clear();
                    for (int i = 0; i < itemList.size(); i++) {
                        if (i == 0) {
                            itemList.get(i).setSelect(true);
                        } else {
                            itemList.get(i).setSelect(false);
                        }
                    }
                    setAddOrderBean(itemList.get(0));
                    data.addAll(itemList);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }


    //获得购卡卡列表信息
    private void getCardInfo() {
        if (TextUtils.isEmpty(storeId)) {
            finish();
        }
        mAppHelper.apiService().getCardInfo(storeId).enqueue(new BaseCallback<ApiResponse<CardListResponseBean.Item>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CardListResponseBean.Item>> call, Response<ApiResponse<CardListResponseBean.Item>> response) {
                CardListResponseBean.Item item = response.body().getData();
                cardId = item.getCard_id();
                cardNameTextView.setText(item.getTicket_name());
                storeNameTextView.setText("店铺名称  " + item.getStadiumname());
                limitTextView.setText(item.getExpire() + "天");
                if (item.getNum().equals("0")) {
                    countTextView.setText("无限次");
                } else {
                    countTextView.setText(item.getNum() + "次");
                }
                moneyTextView.setText(item.getPrice());
                contentTextView.setText(StringUtils.fromHtml(item.getInfo()));
                totalMoneyTextView.setText(item.getPrice());
                account = StringUtils.toDouble(item.getAccount());
                paymentAmount = StringUtils.toDouble(item.getPrice());
                balanceTextView.setText(Html.fromHtml("账户余额：" + AMapUtil.colorFontDefaulYellow("¥" + account)));
                setAddOrderBean(item);
            }
        });
    }

    private void getCoachOrderInfo() {
        if (TextUtils.isEmpty(storeId)) {
            finish();
        }
        mAppHelper.apiService().getCoachOrderInfo(storeId).enqueue(new BaseCallback<ApiResponse<CoachOrderBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CoachOrderBean>> call, Response<ApiResponse<CoachOrderBean>> response) {
                CoachOrderBean bean = response.body().getData();
                if (bean != null) {
                    storeNameTextView.setText(bean.getStadiumname());
                    limitTextView.setText("私教卡");
                    countTextView.setText(bean.getCoach_name());
                    moneyTextView.setText(bean.getPrice());
                    unitPrice = StringUtils.toDouble(bean.getPrice());
                    totalMoneyTextView.setText(bean.getPrice());
                    balanceTextView.setText(Html.fromHtml("账户余额：" + AMapUtil.colorFontDefaulYellow("¥" + bean.getAccount())));
                    account = StringUtils.toDouble(bean.getAccount());
                    paymentAmount = StringUtils.toDouble(bean.getPrice());
                }
                totalNumTextView.setText("共1件");
                setAddOrderBean(bean);
            }
        });
    }

    //    新增订单bean
    private void setAddOrderBean(Object object) {
        switch (orderType) {
            case ORDER_CARD:
                CardListResponseBean.Item item = (CardListResponseBean.Item) object;
                addOrderBean.setCard_id(item.getCard_id());
                addOrderBean.setPlace_id(item.getSid());
                addOrderBean.setOrder_type(orderType);
                break;
            case Type.ORDER_RECHARGE:
                CardDetialResponseBean rechargeBean = (CardDetialResponseBean) object;
                addOrderBean.setCard_id(rechargeBean.getCard_id());
                addOrderBean.setOrder_type(orderType);
                addOrderBean.setPlace_id(rechargeBean.getSid());
                break;
            case Type.ORDER_COACH:
                CoachOrderBean coachBean = (CoachOrderBean) object;
                if (coachBean != null) {
                    addOrderBean.setCoach_id(storeId);
                }
                addOrderBean.setOrder_type(orderType);
                addOrderBean.setNum(purchaseNumTextView.getText().toString());
                break;
        }
    }

    //下单
    private void order() {
        if (!orderType.equals(Type.ORDER_RECHARGE)) {
            if (balanceRadioButton.isChecked() && account < paymentAmount) {
                ToastUtil.show($this, "余额不足");
                return;
            }
        }
        addOrder();
    }

    //新增订单
    private void addOrder() {
        mAppHelper.apiService().addOrder(addOrderBean.getOrder_type(),
                addOrderBean.getPlace_id(), addOrderBean.getStart_time(), addOrderBean.getEnd_time(), addOrderBean.getPlan_info(),
                addOrderBean.getCard_id(), addOrderBean.getCoach_id(), addOrderBean.getNum(), addOrderBean.getCorps_id(),
                addOrderBean.getOrder_id(), addOrderBean.getOrder_num(), addOrderBean.getInfo(), addOrderBean.getCategory_id(), "")
                .enqueue(new BaseCallback<ApiResponse<OrderResponseBean>>() {
                    @Override
                    public void onSuccess(Call<ApiResponse<OrderResponseBean>> call, Response<ApiResponse<OrderResponseBean>> response) {
                        OrderResponseBean bean = response.body().getData();
                        if (orderType.equals(Type.ORDER_CARD)) {
                            IConstants.orderId = bean.getOrder_id();
                            IConstants.orderType = Type.ORDER_CARD;
                        }
                        if (!TextUtils.isEmpty(bean.getOrder_id())) {
                            if (wachatRadioButton.isChecked()) {
                                wachatPayOrder(bean.getOrder_id());
                            } else if (alipayRadioButton.isChecked()) {
                                getAlipayOrderInfo(bean.getOrder_id());
                            } else {
                                payOrder(bean.getOrder_id());
                            }
                        }
                    }
                });
    }

    //订单付款
    private void payOrder(String orderId) {
        mAppHelper.apiService().payOrder(orderId, "1").enqueue(new BaseCallback<ApiResponse<Boolean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<Boolean>> call, Response<ApiResponse<Boolean>> response) {
                if (response.body().getData()) {
                    PaidSuccessActivity.startActivity($this, StringUtils.toInt(orderType));
                }
            }
        });
    }

    //获得订单详情
    private void getOrderInfo(final String orderId) {
        String orderType = "";
        if (from.equals(OrderType.from_coupon)) {
            String tempType = getIntent().getStringExtra(EXTRA_TYPE);
            if (tempType.equals(Type.STADIUM_TYPE)) {
                orderType = Type.ORDER_MORE_STDIUM;
            } else if (tempType.equals(Type.STADIUM_YUE)) {
                orderType = Type.ORDER_YUE;
            } else if (tempType.equals(Type.STADIUM_FELLOW)) {
                orderType = Type.ORDER_FELLOW;
            } else {
                orderType = getIntent().getStringExtra(EXTRA_TYPE);
            }
        } else {
            orderType = getIntent().getStringExtra(EXTRA_TYPE);
        }

        final String finalOrderType = orderType;
        mAppHelper.apiService().getOrderInfo(orderId).enqueue(new BaseCallback<ApiResponse<OrderInfoResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<OrderInfoResponseBean>> call, Response<ApiResponse<OrderInfoResponseBean>> response) {
                final OrderBean bean = OrderBean.dataChange(response.body().getData(), finalOrderType);
                totalMoneyTextView.setText(bean.getPayMoney());
                totalNumTextView.setText("共" + bean.getTotalCount() + "件");
                contentTextView.setText(bean.getInfo());
                if (!finalOrderType.equals(Type.RECHARGE_TYPE)) {
                    balanceTextView.setText(Html.fromHtml("账户余额：" + AMapUtil.colorFontDefaulYellow("¥" + bean.getAccount())));
                }
                if (from.equals(OrderType.from_coupon) || finalOrderType.equals(Type.STADIUM_TYPE) || finalOrderType.equals(Type.STADIUM_TICKET_TYPE)
                        || finalOrderType.equals(Type.STADIUM_YUE) || finalOrderType.equals(Type.STADIUM_FELLOW)
                        || finalOrderType.equals(Type.ORDER_ONE_STDIUM) || finalOrderType.equals(Type.ORDER_MORE_STDIUM)) {
                    fromCouponInit();
                    if (bean.getDetailList() != null && bean.getDetailList().size() > 0) {
                        gridView.setAdapter(new QuickTypeAdapter<MyOrderBean.DetialStadium>($this, bean.getDetailList(), RES_ID_ITEM_STADIUM) {

                            @Override
                            public void bindData4View(ViewHolder holder, MyOrderBean.DetialStadium data, int type, int pos) {
                                holder.setText(RES_ID_ITEM_STADIUM_NAME, bean.getStadiumName());
                                holder.setText(RES_ID_ITEM_STADIUM_PLACE, data.getPlaceName());
                                holder.setText(RES_ID_ITEM_STADIUM_MONEY, data.getPrice());
                                holder.setText(RES_ID_ITEM_STADIUM_TIME, data.getTime());
                                if (bean.getDetailList().size() == 1) {
                                    holder.setVisibility(RES_ID_ITEM_STADIUM_GREY_LINE, View.GONE);
                                } else {
                                    holder.setVisibility(RES_ID_ITEM_STADIUM_GREY_LINE, View.VISIBLE);
                                }
                            }
                        });
                        IConstants.orderId = orderId;
                        IConstants.orderType = finalOrderType;
                    }
                } else if (finalOrderType.equals(Type.BUY_CARD_TYPE)) {
                    IConstants.orderId = orderId;
                    IConstants.orderType = finalOrderType;
                    fromCardInit();
                    cardId = bean.getCardDetail().getCardId();
                    cardNameTextView.setText(bean.getCardDetail().getCardName());
                    storeNameTextView.setText(bean.getStadiumName());
                    limitTextView.setText(bean.getCardDetail().getExpire() + "天");
                    if (bean.getCardDetail().getCount().equals("0")) {
                        countTextView.setText("无限次");
                    } else {
                        countTextView.setText(bean.getCardDetail().getCount() + "次");
                    }
                    moneyTextView.setText(bean.getCardDetail().getPrice());
                    totalMoneyTextView.setText(bean.getPayMoney());
                } else if (finalOrderType.equals(Type.RECHARGE_TYPE)) {
                    fromRechargeInitForOrder();
                    storeNameTextView.setText(bean.getStadiumName());
                    moneyTextView.setText(bean.getPayMoney());
                    totalMoneyTextView.setText(bean.getPayMoney());
                } else if (finalOrderType.equals(Type.COACH_TYPE)) {
                    fromCoachInitForOrder();
                    cardNameTextView.setText(bean.getStadiumName());
                    limitTextView.setText(bean.getDetailCoach().getCoachName());
                }
            }

            @Override
            public void onError(Call<ApiResponse<OrderInfoResponseBean>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });
    }

    //    微信支付
    private void wachatPayOrder(String orderId) {
        mAppHelper.apiService().appWxpay(orderId).enqueue(new BaseCallback<ApiResponse<WxPayBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<WxPayBean>> call, Response<ApiResponse<WxPayBean>> response) {
                WxPayBean bean = response.body().getData();
                if (bean != null) {
                    PayUtils payUtils = new PayUtils($this);
                    payUtils.payWx(bean);
                }
            }
        });
    }

    //支付宝支付
    private void getAlipayOrderInfo(String orderId) {
        mAppHelper.apiService().getAlipayOrderInfo(orderId).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                if (!TextUtils.isEmpty(response.body().getData())) {
                    PayUtils payUtils = new PayUtils($this, "alipay");
                    payUtils.alipay(response.body().getData());
                }
            }
        });
    }

    private void leaveMesssage(String orderId) {
        if (TextUtils.isEmpty(remarkEditText.getText().toString())) {
            return;
        }
        mAppHelper.apiService().leaveMesssage(orderId, remarkEditText.getText().toString()).enqueue(new BaseCallback<ApiResponse<ActivityHistoryResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<ActivityHistoryResponseBean>> call, Response<ApiResponse<ActivityHistoryResponseBean>> response) {

            }

            @Override
            public void onError(Call<ApiResponse<ActivityHistoryResponseBean>> call, Throwable t) {
            }

            @Override
            public void onFailure(Call<ApiResponse<ActivityHistoryResponseBean>> call, Throwable t) {
            }
        });
    }
}
